package cn.mifengkong.appcommon;

import android.app.Application;

import com.growingio.android.sdk.collection.Configuration;
import com.growingio.android.sdk.collection.GrowingIO;
import com.hyphenate.helpdesk.easeui.UIProvider;
import com.mifengkong.frtools.util.FRDebugMode;
import com.mifengkong.im.DemoApplication;
import com.mifengkong.statistic.talkingdata.TalkingData;
import com.moxie.client.manager.MoxieSDK;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.Map;

import cn.mifengkong.appcommon.util.ChannelUtil;
import cn.mifengkong.appcommon.util.ProcessUtil;

/**
 * Created by jack on 2017/8/31
 */

public class CommonApp {

    public static final String TALKING_DATA_ANALYTICS_APP_ID = "talking_data_analytics_app_id";//talkingdata  分析
    public static final String TALKING_DATA_TRACKING_APP_ID = "talking_data_tracking_app_id";//talkingData  广告
    public static final String UMENG_SHARE_WEIXIN_ID = "umeng_share_weixin_id";//友盟分享  微信  Id
    public static final String UMENG_SHARE_QQZONE_ID = "umeng_share_qqzone_id";//友盟分享  QQ  Id
    public static final String UMENG_SHARE_SINAWEIBO_ID = "umeng_share_sinaweibo_id";//友盟分享  微博  Id
    public static final String UMENG_SHARE_WEIXIN_SCERET = "umeng_share_weixin_sceret";//友盟分享  微信  sceret
    public static final String UMENG_SHARE_QQZONE_SCERET = "umeng_share_qqzone_sceret";//友盟分享  QQ  sceret
    public static final String UMENG_SHARE_SINAWEIBO_SCERET = "umeng_share_sinaweibo_sceret";//友盟分享  微博  sceret
    public static final String UMENG_SHARE_SINAWEIBO_LINK = "umeng_share_sinaweibo_link";//友盟分享  微博链接
    public static final String CRASH_REPORT_KEY = "crash_report_key";//bugly  key
    public static final String IM_APP_KEY = "im_app_key";//环信的APPkey
    public static final String IM_APP_USER_AVATAR = "im_app_user_avatar";//环信的用户头像

    private CommonApp() {

    }

    public static void init(Application context, Map<String, String> dataMap) {
        if (dataMap == null || dataMap.size() < 10) {
            throw new IllegalArgumentException("配置信息不正确，请检查");
        }
        try {
            //growingIO
            GrowingIO.startWithConfiguration(context, new Configuration()
                    .useID()
                    .trackAllFragments()
                    .setDebugMode(FRDebugMode.isDebugMode()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Moxie
        MoxieSDK.init(context);

        //bugly
        initBugly(context, dataMap);
        //umeng分享


        UMShareAPI.get(context);
        setDifferentSysData(context, dataMap);
    }

    //    //第三方SDK的一些配置
    private static void setDifferentSysData(Application context, Map<String, String> dataMap) {
        String weiXinKey = dataMap.get(UMENG_SHARE_WEIXIN_ID);
        String weiXinSecret = dataMap.get(UMENG_SHARE_WEIXIN_SCERET);

        String qqZoneKey = dataMap.get(UMENG_SHARE_QQZONE_ID);
        String qqZoneSecret = dataMap.get(UMENG_SHARE_QQZONE_SCERET);

        String weiBoKey = dataMap.get(UMENG_SHARE_SINAWEIBO_ID);
        String weiBoSecret = dataMap.get(UMENG_SHARE_SINAWEIBO_SCERET);
        String weiBoLink = dataMap.get(UMENG_SHARE_SINAWEIBO_LINK);

        String dataAnalyticsAppId = dataMap.get(TALKING_DATA_ANALYTICS_APP_ID);
        String dataTrackingAppId = dataMap.get(TALKING_DATA_TRACKING_APP_ID);

        String imAppKey = dataMap.get(IM_APP_KEY);
        String imAppUserAvatar = dataMap.get(IM_APP_USER_AVATAR);

        PlatformConfig.setWeixin(weiXinKey, weiXinSecret);
        PlatformConfig.setQQZone(qqZoneKey, qqZoneSecret);
        PlatformConfig.setSinaWeibo(weiBoKey, weiBoSecret, weiBoLink);
        //talkingdata
        String channelName = ChannelUtil.getChannelName(context);
        TalkingData.initAppAnalytics(context, dataAnalyticsAppId, channelName);
        TalkingData.initAdTracking(context, dataTrackingAppId, channelName);

        //环信
        initHuanXin(context, imAppKey, imAppUserAvatar);
    }

    private static void initBugly(Application context, Map<String, String> dataMap) {
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = ProcessUtil.getProcessName(context, android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        String crashAppId = dataMap.get(CRASH_REPORT_KEY);
        CrashReport.initCrashReport(context, crashAppId, FRDebugMode.isDebugMode());
    }

    /**
     * @param context
     * @param imAppKey        环信appkey
     * @param imAppUserAvatar 环信用户头像的地址
     */
    private static void initHuanXin(Application context, String imAppKey, String imAppUserAvatar) {
        DemoApplication.init(context, imAppKey, imAppUserAvatar);

        // Kefu EaseUI的初始化
        UIProvider.getInstance().init(context);
    }
}